package com.example.seabedmusic.views;

import android.Manifest;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.seabedmusic.R;
import com.example.seabedmusic.adapter.NotificationSettingsAdapter;
import com.example.seabedmusic.entity.NotificationSettingItem;
import java.util.ArrayList;
import java.util.List;

public class PrivacySettingsActivity extends AppCompatActivity implements NotificationSettingsAdapter.OnSwitchChangeListener {

    private RecyclerView rvPrivacy;
    private NotificationSettingsAdapter adapter;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_list);

        preferences = getSharedPreferences("privacy_settings", MODE_PRIVATE);
        initViews();
        setupPrivacyList();
    }

    private void initViews() {
        rvPrivacy = findViewById(R.id.rvSettings);
        rvPrivacy.setLayoutManager(new LinearLayoutManager(this));
        setTitle("隐私与权限");
    }

    private void setupPrivacyList() {
        List<NotificationSettingItem> items = new ArrayList<>();

        items.add(new NotificationSettingItem("位置权限", "获取附近用户和活动信息",
                hasLocationPermission()));
        items.add(new NotificationSettingItem("存储权限", "访问本地音乐和下载文件",
                hasStoragePermission()));
        items.add(new NotificationSettingItem("麦克风权限", "用于语音搜索和K歌功能",
                hasMicrophonePermission()));
        items.add(new NotificationSettingItem("个性化推荐", "根据听歌喜好推荐内容",
                preferences.getBoolean("personalized_recommendation", true)));
        items.add(new NotificationSettingItem("数据收集", "帮助改进产品体验",
                preferences.getBoolean("data_collection", true)));
        items.add(new NotificationSettingItem("展示在线状态", "向好友展示在线状态",
                preferences.getBoolean("show_online_status", true)));

        adapter = new NotificationSettingsAdapter(items);
        adapter.setOnSwitchChangeListener(this);
        rvPrivacy.setAdapter(adapter);
    }

    @Override
    public void onSwitchChanged(int position, boolean isChecked) {
        NotificationSettingItem item = adapter.getItemAtPosition(position);
        if (item != null) {
            switch (item.getTitle()) {
                case "位置权限":
                    requestLocationPermission();
                    break;
                case "存储权限":
                    requestStoragePermission();
                    break;
                case "麦克风权限":
                    requestMicrophonePermission();
                    break;
                case "个性化推荐":
                    preferences.edit().putBoolean("personalized_recommendation", isChecked).apply();
                    break;
                case "数据收集":
                    preferences.edit().putBoolean("data_collection", isChecked).apply();
                    break;
                case "展示在线状态":
                    preferences.edit().putBoolean("show_online_status", isChecked).apply();
                    Toast.makeText(this, isChecked ? "在线状态已开启" : "在线状态已隐藏", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    private boolean hasLocationPermission() {
        return ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
    }

    private boolean hasStoragePermission() {
        return ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    }

    private boolean hasMicrophonePermission() {
        return ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
    }

    private void requestLocationPermission() {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1001);
    }

    private void requestStoragePermission() {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1002);
    }

    private void requestMicrophonePermission() {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.RECORD_AUDIO}, 1003);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "权限已授予", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "权限被拒绝", Toast.LENGTH_SHORT).show();
        }
        // 刷新列表
        setupPrivacyList();
    }
}